#ifndef __PING_H_
#define __PING_H_

#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
#include <QTimer>
#include <QList>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <qdebug.h>
#include <qfile.h>
#include <QFile>
#include <sys/ioctl.h>
#include <unistd.h>
#include <QString>
#include <QNetworkInterface>
#include <QList>
#include <QFrame>
#include <QThread>

#include <QWidget>
#include "jwidget/jdialog.h"
#include <QDialog>
#include "weiqiantools/serialport.h"
#include "weiqiantools/weiqianfunctions.h"
#include "weiqiantools/serialportreadthread.h"
#include "InputMethod/inputmethod.h"

extern "C"
{
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <unistd.h>
	#include <stdio.h>
	#include <netinet/in.h>
	#include <netinet/ip.h>
	#include <netinet/ip_icmp.h>
	#include <signal.h>
	#include <arpa/inet.h>
	#include <errno.h>
	#include <sys/time.h>
	#include <string.h>	
	#include <netdb.h>
	#include <pthread.h>
	#include <sys/ioctl.h>
	#include <net/if.h>
	#include <net/route.h>
	#include <fcntl.h>
	#include <sys/un.h> 
	#include <unistd.h>
	#include <sys/socket.h>
}

extern long long get_file_size(const char *path);
extern float get_time_time(const char *str);
extern int isDigitStr(QString src);



#define MAY_FIFO "/tmp/pingfifo"


#if 0
class ReadThread : public QThread
{
	Q_OBJECT

public:
	explicit ReadThread(QWidget *parent = 0);
	~ReadThread();

	bool alive;
	int fifo;

signals:
    void recvdata(const QString data);

public slots:

protected:
    void run();

	

};

#endif




class PingThread : public QThread
{
	Q_OBJECT

public:
	explicit PingThread(QObject *parent = 0);



	void setParam(QString ip, QString interval, QString size);
	u_int16_t Compute_cksum(struct icmp *);
	void SetICMP(u_int16_t );
	double GetRtt(struct timeval *, struct timeval *);
	int unpack(struct timeval *);
	void Statistics();
	void SendPacket(int);
	int RecvePacket();
	void clearData();

	char SendBuffer[65536];
	char RecvBuffer[65536];
	long nSend;
	long nRecv;  
	
	struct timeval FirstSendTime;	 
	struct timeval LastRecvTime;

	char send_buff[1024]; 
	int connect_fd;  
	int pack_size;	 
	int sock_icmp;				 
	
	QString IP;
	
	struct hostent * pHost;
	struct timeval tv;
	struct sockaddr_in dest_addr; 	 
	in_addr_t inaddr;		 


	fd_set write_fd;
	fd_set read_fd;
	struct timeval tv1;
	struct timeval tv2;


	double min;
	double avg;
	double max;
	double mdev;

	bool alive;


protected:
	void run();

signals:
	void info(long send, long recv);

	void data(QString);


};



class PingTool : public QDialog
{
    Q_OBJECT

public:
	PingTool(QWidget *parent = 0);
	~PingTool();

	QPushButton *start_bt;
	QPushButton *stop_bt;
	QPushButton *one_key_getip_bt;
	QPushButton *exit_bt;
	QPushButton *read_log_bt;
	QPushButton *set_log_path;

	QLineEdit *ip_edit;
	QLineEdit *timing_edit;
	QLineEdit *count_edit;
	QLineEdit *period_edit;
	QLineEdit *pack_size_edit;

	QLineEdit *send_pack_edit;
	QLineEdit *loss_pack_edit;
	QLineEdit *loss_persent_edit;
	QLineEdit *total_time_edit;

#if 0
	QLabel *send_data_label;
	QLabel *recv_data_label;
	QLabel *loss_data_label;
	QLabel *loss_percent_data_label;
	

	QLabel *start_time_data_label;
	QLabel *end_time_data_label;
	QLabel *total_time_data_label;
	QLabel *cur_time_data_label;
#endif

	QRadioButton    *timingRadioBut;
	QRadioButton    *rationRadioBut;
	
	QComboBox *typeCombo;
	QComboBox *unitCombo;
	QString log_path;
	
	QProcess *process;
	PingThread *thread;
	
	QString tmp_unit;

	InputMethod *im;
	QPlainTextEdit *debug_edit;

	int mode;
	int timing;
	int unit;
	int count;
	int period;
	int size;


	QTime		totalTime;
	QTimer      *tickTimer;
	QTimer      *barTimer;
	QStringList options; 
	
	QString cmd; 
	QString interface;

private:
	
	
	bool check_ping_vaile();
	void debug_add_text(const char *str);
	
	void debug_add_warning(const char *str);
	bool ping_socket_init(void);
	void debug_add_text(QString str);


private slots:
	void start_ping_test();
	void exit_ping_test();
	void stop_ping_test();
	void one_key_getip();
	void ping_mode_timing();
	void ping_mode_ration();
	void read_ping_log();
	void pingreceiveData(const QString str);
	void createImConnect(QString ObjName);
	void auto_draw_bar(void);
	void setLogPath(void);
	void comboBoxValueChanged(void);
	void pingReadOut(QString);
	void pinginfo(long, long);


};





#endif
